import Search from "../../config/search";
Page({
  data: {
    pageTopHeight: wx.getWindowInfo().statusBarHeight,
    Search: null,
    roomList: [], // 房间列表
    modalCole: 0,
    modal: false,
  },

  onLoad(option) {
    const _this = this
    // 获取网络状态 提示网络
    const network = wx.getStorageSync("network");
    if(!network){
      this.setData({modalCole:3})
      wx.setStorageSync("network",true);
    }
    // 网络提示
    const app = getApp();
    app.netWork.netWorkCallback((networkObject) => {
      if(networkObject.isConnected === false || (networkObject.networkType !== "wifi" && networkObject.networkType !== 'unknown')){
        _this.setData({modalCole:3})
      }
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */

  close(){
    this.setData({modalCole:0})
  },
  onShow() {
    if (this.data.Search) {
      this.data.Search.destroy();
    }
    this.setData({
      Search: new Search(this),
    });
    // 提示
    const { showToastTitle } = getApp().globalData;
    if (showToastTitle) {
      wx.showToast({
        title: showToastTitle,
        icon: "none",
        mask: true,
        duration: 3000,
      });
      // 清空
      getApp().globalData.showToastTitle = "";
      setTimeout(() => {
        this.data.Search.createGetGame();
      }, 3000);
    } 
    // else {
    //   this.data.Search.createGetGame();
    // }
  },
  // 销毁
  onUnload () {
    this.destroy();
  },
  // 隐藏
  onHide() {
    this.destroy();
  },
  
  // 不返回房间 退出
  goBackNot() {
    this.data.Search.goBackNot();
  },
  // 确认返回房间
  goBackOk() {
    this.data.Search.goBackOk();
  },

  // 用户点击加入房间
  joinRoom(e) {
    const userInfo = wx.getStorageSync("userInfo");
    if(!userInfo || !userInfo.userName || !userInfo.userPicture){
      this.setData({modal: true})
      return
    }
    const game = e.currentTarget.dataset.game;
    if (game.count === game.userCount && game.count !== 0) {
      wx.showToast({
        title: "房间已满",
        icon: "none",
        duration: 3000,
      });
      return;
    }
    const ip = game.gameIp;
    wx.navigateTo({
      url: `/pages/cRoom/cRoom?ip=${ip}`,
    });
  },

  // 查找
  searchGetGame() {
    const _this = this
    wx.getNetworkType({
      complete(res){
        if(res.errMsg !== "getNetworkType:ok"){
          _this.data.Search.SearchGetGame();
        } else {
           if(res.networkType !== "wifi"){
            _this.setData({modalCole:3})
           }else{
             _this.data.Search.SearchGetGame();
           }
        }
      }
    });
  },

  // 跳转到 创建房间
  createRoom() {
    const userInfo = wx.getStorageSync("userInfo");
    if(!userInfo || !userInfo.userName || !userInfo.userPicture){
      this.setData({modal: true})
      return
    }
    wx.navigateTo({
      url: "/pages/create/create",
    });
  },

  // 销毁卸载
  destroy() {
    this.data.Search && this.data.Search.destroy();
    this.setData({ Search: null });
  },

  // 分享转发
  onShareAppMessage() {
    return {
      title: "速来抓狼",
      path: `/pages/home/home`,
    };
  },
});
